/**
 * Created by lixiaodi on 2019/06/06.
 */
mini.parse();
mini_debugger = false;
var tree = mini.get("tree1");       //得到树
var grid = mini.get("datagrid1");   //得到表
var gPersonData = [];				//所有人员数据
var perIds;							//所有父页面被选中的人员id
//树结构显示
$(function(){
    httpRequest();

});
//父页面给子页面传值
function SetData(data){
	perIds = data.addData;
}

//选择节点
tree.on("nodeselect", function (e) {
    var nodes = e.node;
    if (nodes) {
        var depId = nodes.pk;
        var types = nodes.id.substring(0,2);
        httpRequest(depId,types);
    } else {
        grid.setData([]);
        grid.setTotalCount(0);
    }
});
//保存
function onSave() {
    var rows = grid.getSelecteds();
    console.log(rows);
    CloseWindow("save");
    // if(rows){
    // 	CloseWindow("save");
    // }

}
//向父页面传数据
function GetData(){
	var rows = grid.getSelecteds();
    return rows;

}
//刷新
function refresh(){
	var selectNode = tree.getSelectedNode();
	var nodes = selectNode;
	var depId = nodes.pk;
    var types = nodes.id.substring(0,2);
    httpRequest(depId,types);
}
//点击树节点请求数据的请求
function httpRequest(depId,types){
    $.ajax({
        // url: ajaxUrl + "/TreeController/getAllTree.do",
        url: ajaxUrl + "/formltems/formTree.do?formListId=29",
        type:"post",
        success:function (data) {
            console.log(data);
            $.ajax({
                url: ajaxUrl + "/formList/findByFormListId.do?formListId=29"  ,
                type: "post",
                dataType: "json",
                success:function(textFieldData) {
                    console.log(typeof textFieldData);
                    textField=textFieldData;
                    // textField = $.parseJSON(textFieldData.data).selecteds[0].cTableField;
                    console.log(textField,'树形textField');
                    tree.set({
                        textField: 'name'
                    });
                    tree.setData(data);
                    var clickNodeFlag = 0;
                    tree.on("nodeselect", function (e) {
                        console.log(e);
                        clickNodeFlag++;
                        treeSelected = e.selected;
                        nodes = tree.getAncestors(treeSelected);
                        nodes.push(treeSelected);
                        $.ajax({
                            url: ajaxUrl + "/formltems/selectArchives.do?formListId=29",
//                                url:"http://192.168.31.65:8080/bpm/formltems/selectArchives.do",
                            type: "post",
                            dataType: "json",
                            data: {

                                condition: e.node.tabName,
                                conValue: e.node.id
                            },
                            success: function(data){
                                console.log(data);
                                grid_data = data.data;
                                grid.setData(grid_data);
                            },
                            error : function(data){
                                alert("我靠")
                            }
                        });
                    })
                }
            })


        },
        error: function (data) {
            mini.alert("失败");
        }
    });
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
	onSave();
}
function onCancel(e) {
    CloseWindow("cancel");
}